package com.cdl.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.cdl.library.R;


public class MyTextView extends TextView {

    private int mMinWidth;
    private int drawableWidth;
    private int drawableHeight;
    private Drawable left, right, top, bottom;

    public MyTextView(Context context) {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        mMinWidth = ta.getDimensionPixelSize(R.styleable.MyTextView_min_width, 0);
        drawableWidth = ta.getDimensionPixelSize(R.styleable.MyTextView_drawable_width, 0);
        drawableHeight = ta.getDimensionPixelSize(R.styleable.MyTextView_drawable_height, 0);
        ta.recycle();
        if (drawableWidth > 0 || drawableHeight > 0) {
            setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        }
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int minWidthSpec = widthMeasureSpec;
        if (mMinWidth > 0 && widthSize < mMinWidth) {
            minWidthSpec = MeasureSpec.makeMeasureSpec(mMinWidth, MeasureSpec.EXACTLY);
        }
        super.onMeasure(minWidthSpec, heightMeasureSpec);

    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        if (left != null) {
            left.setBounds(0, 0, drawableWidth > 0 ? drawableWidth : left.getIntrinsicWidth(), drawableHeight > 0 ? drawableHeight : left.getIntrinsicHeight());
        }
        if (right != null) {
            right.setBounds(0, 0, drawableWidth > 0 ? drawableWidth : right.getIntrinsicWidth(), drawableHeight > 0 ? drawableHeight : right.getIntrinsicHeight());
        }
        if (top != null) {
            top.setBounds(0, 0, drawableWidth > 0 ? drawableWidth : top.getIntrinsicWidth(), drawableHeight > 0 ? drawableHeight : top.getIntrinsicHeight());
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, drawableWidth > 0 ? drawableWidth : bottom.getIntrinsicWidth(), drawableHeight > 0 ? drawableHeight : bottom.getIntrinsicHeight());
        }
        setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        super.setCompoundDrawables(left, top, right, bottom);
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    public Drawable getLeftDrawable() {
        return left;
    }

    public Drawable getTopDrawable() {
        return top;
    }

    public Drawable getRightDrawable() {
        return right;
    }

    public Drawable getBottomDrawable() {
        return bottom;
    }
}
